package com.taxshare.seller.util;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName: MwDecryptUtil
 * @Description:
 * @author: luozhongyi
 * @date: 2021/6/4 17:41
 */
public class MwDecryptUtil {

    public static Cipher cipher;
    static final String KEY_ALGORITHM = "AES";
    static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
    private static byte[] key = {-1, 66, -82, -107, 11, 81, -54, 21, 33, -116, 79, -86, -36, -110, -86, -19, -3, -21, 78, 13, -84, -128, 82, -1, 69, -112, -123, -54, -53, -97, -81, -67};
    private static byte[] vec = {-14, 31, -84, 91, 44, -64, -87, -48, -68, -81, -122, -103, -83, -86, -5, 96};

    /**
     * 密文解析
     *
     * @param
     * @return
     * @Author: laiyutao
     * @Date: 2021-01-18 19:13
     */
    public static String desMw(String mw) {
        byte[] keys_buffer = new byte[0];
        try {
            keys_buffer = getKey();
            return DesMwToYw(mw, keys_buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mw;
    }


    private static String DesMwToYw(String mw, byte[] buffer) throws Exception {
        byte[] buffer2 = new byte[]{-81, 82, -34, 69, 15, 88, -51, 16, 35, -117, 69, 106, 16, -112, -81, -67};
        byte[] buffer3 = new byte[]{2, -81, -68, -85, -52, -112, 57, -112, -68, -81, -122, -103, -83, -86, -5, 96};
        byte[] bytes = DatatypeConverter.parseBase64Binary(mw);
        byte[] buffer5 = Decrypt(bytes, buffer2, buffer3, buffer);
        return new String(buffer5, "utf8");
    }

    private static byte[] Encrypt(byte[] byte_0, byte[] byte_1, byte[] byte_2) throws Exception {
        return encrypt(byte_0);
    }

    private static byte[] Decrypt(byte[] byte_0, byte[] byte_1, byte[] byte_2, byte[] byte_3) throws Exception {
        return decrypt(byte_0, byte_1, byte_2);
    }

    private static byte[] getKey() throws Exception {
        byte[] buffer = Encrypt(String.valueOf("123456789").getBytes("UTF-8"), null, null);
        return buffer;
    }

    public static Cipher getInstance() {
        if (cipher == null) {
            try {
                cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            }
        }
        return cipher;
    }

    public static synchronized byte[] encrypt(byte[] str)
            throws Exception {
        cipher = getInstance();
        SecretKeySpec aesKey = new SecretKeySpec(key, KEY_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(vec));
        byte[] encrypt = cipher.doFinal(str);
        return encrypt;
    }

    public static synchronized byte[] decrypt(byte[] str, byte[] ckey, byte[] val)
            throws Exception {
        cipher = getInstance();
        SecretKeySpec aesKey = new SecretKeySpec(ckey, KEY_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(val));
        byte[] decrypt = cipher.doFinal(str);
        return decrypt;
    }
}
